package com.upc.toss;

import com.upc.toss.slice.ChangeAbilitySlice;
import ohos.agp.components.*;

import java.util.List;

public class SettingProvider extends BaseItemProvider {
    private List<SettingItem> list;
    private ChangeAbilitySlice slice;

    public SettingProvider(List<SettingItem> list, ChangeAbilitySlice slice) {
        this.list = list;
        this.slice = slice;
    }

    public int getCount() {
        return list == null ? 0 : list.size();
    }
    @Override
    public Object getItem(int position) {
        if (list != null && position >= 0 && position < list.size()){
            return list.get(position);
        }
        return null;
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public Component getComponent(int position, Component convertComponent, ComponentContainer componentContainer) {
        final Component cpt;
        ViewHolder holder;
        if (convertComponent == null) {
            cpt = LayoutScatter.getInstance(slice).parse(ResourceTable.Layout_layout_item_setting, null, false);
            holder = new ViewHolder(cpt);
            cpt.setTag(holder);
        } else {
            cpt = convertComponent;
            holder = (ViewHolder) cpt.getTag();
        }
        SettingItem sampleItem = list.get(position);
        holder.eqId.setText(sampleItem.getEquipmentTitle());
        holder.eqName.setText(sampleItem.getEquipmentName());
        return cpt;
    }
    class ViewHolder {
        Text eqId;
        Text eqName;
        public ViewHolder(Component component) {
            eqId = (Text) component.findComponentById(ResourceTable.Id_text_equipment_tiele);
            eqName = (Text) component.findComponentById(ResourceTable.Id_text_equipment_info);
        }
    }
}
